$(function(){
    var nameT = "F";
    var passwordT = "F";
    var registerName = "F";
    //登录用户名
    $("#username").on("blur",function(){
        $.ajax({
            type : "POST",
            url : "/blog/server/login",
            data : {
                "key":"checkName",
                "username":$("#username").val().replaceAll(" ","")
            },
            dataType : "Text",
            success : function(data){
                if(data == 'F'){
                    $("#nameTips").html("<span class='tips_userF'>用户不存在</span>");
                    setTimeout("$('#nameTips').html(' ')","2000"); //6000毫秒后执行testFunction()函数，只执行一次。
                    nameT = "F";
                }else if(data == 'T'){
                    $("#nameTips").html("<span class='tips_userT'>√</span>")
                    nameT = "T";

                }
            },
            error:function(){
                console.log("未知错误!");
            }
        })
    })
    //登录按钮
    $("#login").on("click",function () {
        if(nameT == "F"){
            $("#nameTips").html("<span class='tips_userF'>用户不存在</span>");
            setTimeout("$('#nameTips').html(' ')","2000"); //2000毫秒后执行testFunction()函数，只执行一次。
        }else if(nameT == "T"){
            $.ajax({
                type : "POST",
                url : "/blog/server/login",
                data : {
                    "key":"login",
                    "username":$("#username").val().replaceAll(" ",""),
                    "password":$("#password").val().replaceAll(" ","")
                },
                async:false,
                dataType : "JSON",
                success : function(data) {
                    if(data.status == "T"){
                        // alert(data.url);
                        window.location.href = data.url+"?_="+new Date().getTime();

                        // $(document).attr("location",data.url);
                    }else{
                        $("#nameTips").html("<span class='tips_userF'>密码错误</span>");
                        setTimeout("$('#nameTips').html(' ')","2000"); //2000毫秒后执行testFunction()函数，只执行一次。
                    }
                }
            })
        }
    })

    //注册
    $("#register").on("click",function () {
        $(".register").attr("style","display:block");
        $("#registerid").on("blur",function () {
            $.post("/blog/server/login","key=checkName&username="+$("#registerid").val().replaceAll(" ",""),function (data) {
                if(data == "T"){
                    registerName="F";
                    $("#registerid").val("用户名存在!!!");
                    setTimeout("$('#registerid').val(' ')","1000"); //2000毫秒后执行testFunction()函数，只执行一次。
                }else if(data == "F"){
                    registerName = "T";
                }

            },"Text");
        })
    })
    //确认注册
    $("#registers").on("click",function () {
        var test = $("#registerid").val().replaceAll(" ","");
        if(registerName=="T" && test.length!=0){
           var username = $("#registerid").val().replaceAll(" ","");
           var password = $("#registerpwd").val().replaceAll(" ","");
           var sex = $(":radio:checked").val();
           var introduce = $("#introduce").val();
            //hobby
                var yd = $("#yd")[0].checked ? $("#yd").val() : "LL";
                var zj = $("#zj")[0].checked == true ? $("#zj").val() : "LL";
                var ms = $("#ms")[0].checked == true ? $("#ms").val() : "LL";
                var ly = $("#ly")[0].checked == true ? $("#ly").val() : "LL";
                var ecy = $("#ecy")[0].checked == true ? $("#ecy").val() : "LL";
                var yx = $("#yx")[0].checked == true ? $("#yx").val() : "LL";
            $.post("/blog/server/login","key=register&username="+username+"&password="+password+"&sex="+sex+"&introduce="+introduce+"&hobby="+yd+"&hobby="+zj+"&hobby="+ms+"&hobby="+ly+"&hobby="+ecy+"&hobby="+yx,function(data) {
                if(data=="T"){
                    alert("注册成功");
                    registerName=="F";
                } else{
                    alert("注册失败");
                    registerName="F";
                }
            },"Text")
            $(".register").attr("style","display:none");
        } else {
            $("#registerid").val("请输入唯一的用户名！！！");
            setTimeout("$('#registerid').val(' ')","1000"); //2000毫秒后执行testFunction()函数，只执行一次。
        }
    })
    //取消注册
    $("#registersF").on("click",function () {
        $(".register").attr("style","display:none");
    })
    //找回
    $("#forget").on("click",function () {
        $(".forget").attr("style","display:block");
    })
    //确认找回
    $("#find").on("click",function () {
        $(".forget").attr("style","display:none");
        $.post("/blog/server/login","key=findPassword&root="+$("#root").val()+"&username="+$("#userFid").val().replaceAll(" ",""),function (data) {
            if(data.status =="F"){
                $("#userFid").val("请输入正确的用户名和帮助码");
                setTimeout("$('#userFid').val(' ')","1000"); //2000毫秒后执行testFunction()函数，只执行一次。
                $("#root").val(" ");
                $(".forget").attr("style","display:block");
            }else if(data.status == "T"){
                $(".forget").attr("style","display:none");
                alert("您的密码是:"+data.password);
            }
        },"JSON")

    })
    //取消找回
    $("#findF").on("click",function () {
        $(".forget").attr("style","display:none");
    })
})